package idu0080.veebitoorik.web;

import idu0080.veebitoorik.serviceclient.SOAPClient;
import idu0080.yl6_OpenERPServices_client.Invoice;
import idu0080.yl6_OpenERPServices_client.InvoiceLine;
import idu0080.yl6_OpenERPServices_client.Product;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DeliveryServlet
 */
@WebServlet("/delivery")
public class DeliveryServlet extends HttpServlet 
{
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeliveryServlet()
    {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		String selectedInvoiceNumber = null;
	    Invoice selectedInvoice = null;

	    SOAPClient soapClient = new SOAPClient();
	    Double quantity = 0.0;
	    
	    if ((request.getParameter("selectedInvoiceNumber") != null) && (!request.getParameter("selectedInvoiceNumber").equals("")))
	    {
	    	selectedInvoiceNumber = request.getParameter("selectedInvoiceNumber");
	    	selectedInvoice = soapClient.getInvoiceByNumber(selectedInvoiceNumber);
			
			InvoiceLine invoiceLine = (InvoiceLine) selectedInvoice.getListOfInvoiceLines().get(0);
			Product product = soapClient.getProductById(invoiceLine.getProductId());
			request.setAttribute("selectedInvoice", selectedInvoice);
			request.setAttribute("selectedInvoiceNumber", selectedInvoiceNumber);
			request.setAttribute("productName", product.getName());
	    }

	    if ((request.getParameter("quantity") != null) && (!request.getParameter("quantity").equals("")))
	    {
	    	quantity = Double.valueOf(Double.parseDouble(request.getParameter("quantity")));
	    }

	    if ((request.getParameter("what_to_do").equals("registerProductDelivery")) && (quantity > 0.0) && (selectedInvoice != null))
	    {
			int deliveryRegistrationId = soapClient.registerProductDelivery(selectedInvoice.getNumber(), quantity);
			
			if (deliveryRegistrationId > 0)
			{
				request.setAttribute("deliveryId", Integer.toString(deliveryRegistrationId));
				selectedInvoice = soapClient.getInvoiceByNumber(selectedInvoiceNumber);
				request.setAttribute("selectedInvoice", selectedInvoice);
			}
			else
			{
				System.out.println("SOAP_ERROR CODE:" + soapClient.getErrorCode());
				System.out.println("SOAP_ERROR MESSAGE:" + soapClient.getErrorMessage());
				request.setAttribute("SOAP_ERROR_CODE", soapClient.getErrorCode());
				request.setAttribute("SOAP_ERROR_MESSAGE", soapClient.getErrorMessage());
			}

	    }

	    getServletConfig().getServletContext().getRequestDispatcher("/delivery.jsp").forward(request, response);
	}

}
